package datebase.live.access;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import models.UserModel;

import com.trackvoodoo.models.UserDocument.User;
import com.trackvoodoo.models.UserListDocument.UserList;

import database.connector.DatabaseFactory;
import database.live.DOAStatements;
import database.live.MySqlDAO;
import engine.EngineUtils;

/*
 * 
 * User access, and user methods.
 * 
 */

public class UserDAO {

	private UserDAO(){};
	
	
	
	
	public static boolean validatePassword(String loginName, String password)
	{
		MySqlDAO d = (MySqlDAO) DatabaseFactory.getDAO();
		
		ResultSet selectQuery = d.selectQuery("select password from trackvoodoo.user where loginName = '" +loginName+"'");
		
		if (selectQuery != null)
		{
			try{
				selectQuery.next();
				if (selectQuery.getString(1).equals(password))
				{
					return true;
				}
			}catch (Exception e)
			{
				return false;
			}
			finally
			{
				d.closeConnection();
			}
		}
		return false;
	}
	
	public static boolean addUser(String user, String loginName, String password, String avatarLoc)
	{
		MySqlDAO d = (MySqlDAO) DatabaseFactory.getDAO();
		if (d.isOpen())
		{
			try{
				PreparedStatement p = d.getPreparedStatement(DOAStatements.ADD_NEW_USER);
				p.setString(1, user);
				p.setString(2, loginName);
				p.setString(3, password);
				p.setString(5, avatarLoc);
				p.setInt(6,0); //false admin user
				int count = p.executeUpdate();
				if (count > 0)
				{
					return true;
				}	
			}
			catch (SQLException e)
			{
				System.out.println(e.getMessage());
			}
			finally
			{
				d.closeConnection();
			}
		}
		return false;
	}
	
	public static boolean addAdmin(String user, String loginName, String password, String avatarLoc)
	{
		MySqlDAO d = (MySqlDAO) DatabaseFactory.getDAO();
		if (d.isOpen())
		{
			try{
				PreparedStatement p = d.getPreparedStatement(DOAStatements.ADD_NEW_USER);
				p.setString(1, user);
				p.setString(2, loginName);
				p.setString(3, password);
				p.setString(5, avatarLoc);
				p.setInt(6,1); //false admin user
				int count = p.executeUpdate();
				if (count > 0)
				{
					return true;
				}	
			}
			catch (SQLException e)
			{
				System.out.println(e.getMessage());
			}
			finally
			{
				d.closeConnection();
			}
		}
		return false;
	}
	
	public static boolean removeUser(String user)
	{
		MySqlDAO d = (MySqlDAO) DatabaseFactory.getDAO();
		if (d.isOpen())
		{
			try{
				PreparedStatement p = d.getPreparedStatement(DOAStatements.DELETE_USER);
				p.setString(1, user);
				int count = p.executeUpdate();
				if (count > 0)
				{
					return true;
				}	
			}
			catch (SQLException e)
			{
				System.out.println(e.getMessage());
			}
			finally
			{
				d.closeConnection();
			}
		}
		return false;
	}
	
	public static User getAllUserInfo(String userRequest) throws SQLException
	{
		return new UserModel(userRequest).getUser();
	}
	
	public static UserList getAllUsersAsFriends()
	{
		MySqlDAO d = (MySqlDAO) DatabaseFactory.getDAO();
		UserList u = UserList.Factory.newInstance();
		try{
			
			PreparedStatement p = d.getPreparedStatement(DOAStatements.SELECT_ALL_USERS); //iduser, name, joinDate, avatar
			ResultSet rs = p.executeQuery();
			while (rs.next())
			{
				com.trackvoodoo.models.UserListDocument.UserList.User user = u.addNewUser();
				user.setIdRef(rs.getInt(1));
				user.setName(rs.getString(2));
				user.setJoinDate(EngineUtils.dateToCalendar(rs.getDate(3)));
				user.setAvatar(rs.getString(4));			
			}
		}
		catch (SQLException s)
		{
			System.out.println(s.getMessage());
		}
		finally
		{
			d.closeConnection();
		}
		return u;	
	}
	
	public static void addFriend(int userId, int friendId)
	{
		//Check if already exists.
		MySqlDAO d = (MySqlDAO) DatabaseFactory.getDAO();
		try{
			PreparedStatement p = d.getPreparedStatement(DOAStatements.SELECT_FRIENDS);
			p.setInt(1, userId);
			ResultSet rs = p.executeQuery();
			
			boolean found = false;
			while (rs.next())
			{
				if (friendId == rs.getInt(1))
				{
					found = true;
				}
			}
			if (found)
			{
				System.out.println("Already added user: " + userId + " friend: " + friendId);
			}else
			{
				p = d.getPreparedStatement(DOAStatements.ADD_NEW_FRIEND);
				p.setInt(1, userId);
				p.setInt(2, friendId);
				p.executeUpdate();
			}
		}
		catch (SQLException s)
		{
			System.out.println(s.getMessage());
		}
		finally
		{
			d.closeConnection();
		}
		
	}
	
}
